<template>
  <div class="control-cont">
    <div class="left-control">
      <i class="icon"></i>
      <span
        @click="changeTag('1')"
        class="lf-btn"
        :class="tagType == '1' && 'active'"
        >贸易商</span
      >
      /
      <span
        @click="changeTag('2')"
        class="lf-btn "
        :class="tagType == '2' && 'active'"
        >货名</span
      >
      /
      <span
        @click="changeTag('3')"
        class="lf-btn "
        :class="tagType == '3' && 'active'"
        >港口</span
      >
    </div>
    <div class="right-control">
      <div class="rt-btn active" v-for="item in selectCargo3" :key="item.id">
        {{ item.dictValue }}
      </div>
      <!-- <div class="rt-btn active">
        焦 炭
      </div>
      <div class="rt-btn active">
        阿拉斯加份
      </div> -->
      <div
        class="rt-btn "
        :class="{ active: !showMore }"
        @click="handleShowMore"
      >
        {{ showMore ? "确定" : "更多" }}
      </div>
      <div class="select-list" v-show="showMore">
        <div>
          <div
            class="rt-btn"
            :class="{ active: item.checked }"
            v-for="item in cargoData"
            :key="item.id"
            @click="toogleChecked(item)"
          >
            <span>{{ item.dictValue }}</span>
            <span v-show="item.checked" class="ckeck-icon"></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tag: {
      type: [Number, String],
      default: "1"
    },
    cargoList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      cargoData: [],
      showMore: false,
      tagType: ""
    };
  },
  computed: {
    selectCargo3() {
      return this.cargoData.filter(_ => _.checked).slice(0, 3);
    },
    cargoDataInit() {
      return JSON.stringify(
        this.cargoList.map(item => ({ ...item, checked: false }))
      );
    }
  },
  watch: {
    cargoDataInit(newVal) {
      this.cargoData = JSON.parse(newVal);
    }
  },
  created() {
    // 初始化cargoData
    // this.cargoData = this.cargoList.map(item => ({ ...item, checked: true }));
    this.tagType = this.tag;
  },
  methods: {
    // 触发左侧标题切换
    changeTag(tag) {
      this.tagType = tag;
      this.handleEmit();
    },
    // 切换下拉选择item项
    toogleChecked(item) {
      item.checked = !item.checked;
    },
    // 打开下拉选择列表
    handleShowMore() {
      this.showMore = !this.showMore;
      // 关闭下拉选择 发起请求获取数据
      if (!this.showMore) {
        this.handleEmit();
      }
    },
    // 获取选中的商品名称 返回 选中数据 dictKey
    getCargoname() {
      const cargoName = this.cargoData
        .filter(_ => _.checked)
        .map(item => item.dictKey);
      return cargoName;
    },
    // 触发通知
    handleEmit() {
      this.$emit("handleChacked", {
        tag: this.tagType,
        cargoName: this.getCargoname()
      });
    }
  }
};
</script>

<style lang="less" scoped>
.control-cont {
  background: rgba(28, 121, 251, 0.1);
  width: 100%;
  height: 22px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left-control {
    font-size: 16px;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #87bcff;
    .icon {
      display: inline-block;
      width: 8px;
      height: 8px;
      background: linear-gradient(180deg, #3cb1fd 0%, #1c79fb 100%);
      border-radius: 50%;
      margin: 0 9px;
      position: relative;
      top: -2px;
    }
    .lf-btn {
      cursor: pointer;
      display: inline-block;
      position: relative;
      &:hover {
        color: rgba(135, 188, 255, 0.88);
      }
      &.active {
        &::after {
          content: "";
          z-index: 99;
          left: 0;
          bottom: -6px;
          height: 2px;
          width: 100%;
          position: absolute;
          background-color: #87bcff;
          border-radius: 2px;
        }
      }
    }
  }
  .right-control {
    font-size: 10px;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #14275b;
    position: relative;
    &:last-child {
      margin-right: 7px;
    }
    .rt-btn {
      // &. {
      //   width: 15px;
      // }
      cursor: pointer;
      display: inline-block;
      position: relative;
      padding: 0 10px;
      margin-right: 1px;
      box-sizing: border-box;
      &::before {
        content: "";
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: linear-gradient(270deg, #29457a 0%, #537bb2 100%);
        z-index: -1;
        transform: skew(-30deg);
      }
      &.active {
        color: #87bcff;
        font-weight: bold;
      }
      &:hover {
        opacity: 0.9;
      }
    }
    .select-list {
      position: absolute;
      width: 186px;
      height: 0px;
      box-sizing: border-box;
      z-index: 9;
      background: rgba(5, 15, 33, 0.8);
      top: 22px;
      right: 2.5%;
      overflow: hidden;
      animation: growHeight 0.5s linear;
      animation-fill-mode: forwards;
      & > div {
        margin: 0 auto;
        overflow-x: hidden;
        overflow-y: scroll;
        width: 90%;
        max-height: 120px;
        box-sizing: border-box;
        &::-webkit-scrollbar {
          display: none;
        }
        .rt-btn {
          display: block;
          width: 95%;
          margin: 4px auto;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .ckeck-icon {
            display: inline-block;
            width: 8px;
            height: 14px;
            border-color: #87bcff;
            border-style: solid;
            border-width: 0 2px 3px 0;
            transform: rotate(45deg);
            position: relative;
            bottom: 2px;
          }
        }
      }
    }
  }
  @keyframes growHeight {
    0% {
      max-height: 0;
    }
    100% {
      max-height: 120px;
      height: auto;
    }
  }
}
</style>
